package com.lancoo.znbkxx.teacher.ui.fragment


import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lancoo.znbkxx.teacher.Ext.Companion.COURSE_CLASSNAME
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.adapter.PublishClassListAdapter
import com.lancoo.znbkxx.teacher.model.data.ClassFlag
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import com.prolificinteractive.materialcalendarview.*
import java.util.*

/**
 * @author : Admin
 * @date   : 2019/8/2 15:15
 * @desc   :
 */
class CalendarDialog(private val classList: MutableList<ClassFlag>) : AppCompatDialogFragment() {
    var selectedListener: OnDateSelectedListener? = null
    var clickListener: View.OnClickListener? = null
    var text: String = ""
    lateinit var widget: MaterialCalendarView
    lateinit var adapter: PublishClassListAdapter
    var list = mutableListOf<ClassFlag>()

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        this.list = classList
        val view = requireActivity().layoutInflater.inflate(R.layout.dialog_date, null)
        val selectAll = view.findViewById<TextView>(R.id.tv_select_all)
        selectAll.setOnClickListener {
            if (selectAll.tag == "selected") {
                selectAll.text = "全选"
                selectAll.tag = "unselected"
                adapter.selectAll(false)
            } else {
                selectAll.text = "取消全选"
                selectAll.tag = "selected"
                adapter.selectAll(true)
            }
        }
        widget = view.findViewById(R.id.calendar_view_range)
        widget.setOnDateChangedListener(selectedListener)
        val calendar = Calendar.getInstance()
        calendar.time = Date()
        widget.addDecorator(EnableBeforeTodayDecorator())
        widget.state().edit().setMinimumDate(
            CalendarDay.from(
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH) + 1,
                1
            )
        ).commit()
        view.findViewById<TextView>(R.id.btn_cancel).setOnClickListener(clickListener)
        view.findViewById<TextView>(R.id.btn_publish).setOnClickListener(clickListener)
        view.findViewById<TextView>(R.id.tv_title).text = text
        val rv: RecyclerView = view.findViewById(R.id.rv)
        val lm = LinearLayoutManager(context).apply { orientation = RecyclerView.HORIZONTAL }
        adapter = PublishClassListAdapter(R.layout.item_class, classList,null)
        rv.layoutManager = lm
        rv.adapter = adapter
        classList.forEachIndexed { index, classFlag ->
            if (classFlag.CourseClassName == COURSE_CLASSNAME) {
                rv.scrollToPosition(index)
            }
        }
        return AlertDialog.Builder(requireContext()).setView(view).create()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
        val param = this.dialog?.window?.attributes
        param?.width = DensityUtils.dp2px(context, 300f)
        param?.height = WindowManager.LayoutParams.WRAP_CONTENT
        dialog?.window?.attributes = param
    }

    inner class EnableBeforeTodayDecorator : DayViewDecorator {
        override fun shouldDecorate(day: CalendarDay?): Boolean {
            return day!!.isBefore(CalendarDay.today())
        }

        override fun decorate(view: DayViewFacade?) {
            view?.setDaysDisabled(true)
        }

    }
}